package test.TP5;

import java.text.Collator;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Damian
 * Date: 19/10/11
 * Time: 15:30
 * To change this template use File | Settings | File Templates.
 */
public class CollatorSorter {

    public CollatorSorter() {

    }

    public <T> void sort(Collator c, List<T> list) {
        sort(c, list, 0, list.size() - 1);
    }

    protected <T> void sort(Collator c, List<T> list, int left, int N) {
        for (int k = (N - 1) / 2; k >= left; k--) {
            sink(c, list, k, N);
        }
        while (N > left) {
            swap(list, left, N--);
            sink(c, list, left, N);
        }
    }

    private <T>void sink(Collator c, List<T> list, int k, int N) {
        while ((((k + 1) * 2) - 1) <= N) {
            int j = ((k + 1) * 2) - 1;
            if (j < N && (c.compare(list.get(j), list.get(j + 1))) < 0) {
                j++;
            }
            if (c.compare(list.get(k), list.get(j)) > 0) break;
            swap(list, k, j);
            k = j;
        }
    }

     public <T> void swap(List<T> list, int a, int b) {
        list.set(b,list.set(a, list.get(b)));
    }

 }